<?php
namespace ZHMVC\DB\MANAGER;
if (! isset($_SESSION)) {
    session_start();
}

include(dirname(dirname(__FILE__))."/zhconfig/Config.php");
$action = SafeRequest(getPGC('action'), 0);

if ($action != "login1") {
	//echo "<script>alert('登录错误');window.location.href='/".MANAGERFPATH."/index.php';</script>";
}

$adminname = SafeRequest(getPGC('adminname'), 0);
$adminpws = SafeRequest(getPGC('adminpws'), 0);

$sysconn = new \ZHMVC\D\SystemConfig();

$ip=new \ZHMVC\B\TOOL\Ip(); // 实例化未引用的类
$ipaddress=$ip->getIp();

$map=" username= '".$adminname."' ";
$rs=D("zhmvc_master")->where($map)->getLinkAll("",true);
$datas = $rs['datas'];
$rows = $rs['rows'];
if ($rows == 0) {
    $IdWorker = \wantp\Snowflake\IdWorker::getIns();
    $id = $IdWorker->id();
    $sql="insert into `zhmvc_loginlog` (id,username,password,loginip,result,addtime) values ('".$id."','".$adminname."','".$adminpws."','".$ipaddress."','用户名不存在', '".date("Y-m-d G:i:s")."')";
    $sysconn->SqlUpdate($sql);
    echo "<script>alert('用户名不存在');window.location.href='/".MANAGERFPATH."/index.php';</script>";
    
} else {
	$adminpws = md5($adminpws);
	$data = $datas[0];
	if ($data['userpassword'] != $adminpws) {
	    $IdWorker = \wantp\Snowflake\IdWorker::getIns();
	    $id = $IdWorker->id();
		$sql="insert into `zhmvc_loginlog` (id,username,password,loginip,result,addtime) values ('".$id."','".$adminname."','".$adminpws."','".$ipaddress."','密码错误', '".date("Y-m-d G:i:s")."')";
		$sysconn->SqlUpdate($sql);
		echo "<script>alert('密码错误');window.location.href='/".MANAGERFPATH."/index.php';</script>";
	} elseif ($data['state'] == 0) {
	    $IdWorker = \wantp\Snowflake\IdWorker::getIns();
	    $id = $IdWorker->id();
		$sql="insert into `zhmvc_loginlog` (id,username,password,loginip,result,addtime) values ('".$id."','".$adminname."','".$adminpws."','".$ipaddress."','该用户名不允许登录', '".date("Y-m-d G:i:s")."')";
		$sysconn->SqlUpdate($sql);
		echo "<script>alert('该用户名不允许登录');window.location.href='/".MANAGERFPATH."/index.php';</script>";
	} else {	$seedarray = microtime();
		$random = rand(1, 1000);
		$IdWorker = \wantp\Snowflake\IdWorker::getIns();
		$id = $IdWorker->id();
		$_SESSION['adminid'] = $data['id'];
		$_SESSION['adminname'] = $adminname;
		$_SESSION['Master_Power'] = $data['seting'];//后台权限

		$sql="insert into `zhmvc_loginlog` (id,username,password,loginip,result,addtime) values ('".$id."','".$adminname."','".$adminpws."','".$ipaddress."','登录成功！', '".date("Y-m-d G:i:s")."')";
		$sysconn->SqlUpdate($sql);

		Header("Location: /".MANAGERFPATH."/main.php");
		//登录成功
	}
}
